package com.yima.baseproj.utils;

import lombok.extern.log4j.Log4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Component
//@Log4j
public class HttpClientUtil {

    private static final String URl = "http://wo-api.uni-ubi.com";

    public static String sendGetRequest(String appId, String appKey, String timestamp, String sign) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(URl + "/v1/" + appId + "/auth");
        // 设置请求头
        httpGet.setHeader("appKey", appKey);
        httpGet.setHeader("timestamp", timestamp);
        httpGet.setHeader("sign", sign);
        // 发送请求并获取响应
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity responseEntity = response.getEntity();
        // 处理响应数据
        if (responseEntity != null) {
            return EntityUtils.toString(responseEntity); // 返回响应数据
        } else {
            return null; // 返回空值表示没有收到响应数据
        }
    }

    public static String sendPostRequest(String url, String jsonData, String token, String projectGuid) throws Exception {
        URL url1 = new URL(URl + url);
        HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("token", token);
        conn.setRequestProperty("projectGuid", projectGuid);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        os.write(jsonData.getBytes("UTF-8"));
        os.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String output;
        StringBuilder response = new StringBuilder();
        while ((output = br.readLine()) != null) {
            response.append(output);
        }
        br.close();
        return response.toString();
    }
}
